<?php
/**
 * Custom functions that act independently of the theme templates
 *
 * Eventually, some of the functionality here could be replaced by core features
 *
 * @package EverBox
 */

/**
 * Easy SEO
 *
 */
global $everbox_options;
if( class_exists('Easy_SEO') ) {
	$easy_seo_args = array(
		'easy_seo' => !empty($everbox_options['easy_seo']) ? $everbox_options['easy_seo'] : false,
		'front_page_desc' => !empty($everbox_options['easy_seo_front_description']) ? $everbox_options['easy_seo_front_description'] : false,
		'front_page_keywords' => !empty($everbox_options['easy_seo_front_keywords']) ? $everbox_options['easy_seo_front_keywords'] : false,
		'post_desc'	=> !empty($everbox_options['easy_seo_excerpt_description']) ? $everbox_options['easy_seo_excerpt_description'] : false,
		'post_keywords' => !empty($everbox_options['easy_seo_tag_keywords']) ? $everbox_options['easy_seo_tag_keywords'] : false,
	);
	new Easy_SEO($easy_seo_args);
}

/**
 * Change default excerpt more text
 */
function everbox_custom_excerpt_more( $more ) {
	return '...';
}
add_filter('excerpt_more', 'everbox_custom_excerpt_more');

/**
 * Change default excerpt length
 */
function everbox_custom_excerpt_length( $length ) {
    global $everbox_options;
    $custom_length = (!empty($everbox_options['excerpt_length'])) ? $everbox_options['excerpt_length'] : 40;
	return $custom_length;
}
add_filter( 'excerpt_length', 'everbox_custom_excerpt_length', 999 );

/**
 * Post comments
 * @param  string $comment
 * @param  string $args
 * @param  string $depth
 * @return string
 */
function everbox_comments( $comment , $args , $depth ) {

    $GLOBALS['comment'] = $comment;
        $comment_post = get_post($comment->comment_post_ID);
    ?>
    <!-- commment -->
    <li id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class(); ?>>
    	<?php
          $avatar_size = 48;
        ?>
    	<div class="commentator-avatar-wrap">
    		<div class="commentator-avatar">
    			<?php echo get_avatar($comment,$avatar_size); ?>
    		</div>
    	</div>
    	<div class="comment-body">
      		<div class="comment-heading">
      			<span class="comment-author"><?php comment_author(); ?></span>
      			<span>&bull;</span>
      			<span class="comment-meta"><?php echo get_comment_date(); ?></span>
      			<span>&bull;</span>
      			<span class="comment-reply"><?php comment_reply_link(array_merge($args,array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?></span>
      			<?php
				if ( $comment_post ) {
					if ( $comment->user_id === $comment_post->post_author ) {
				?>
				<span>&bull;</span>
				<span class="comment-byauthor"><?php _e('Author', 'twenty'); ?></span>
				<?php
					}
				}
				?>

			</div>
      		<?php if ($comment->comment_approved == '0') : ?>
            <div class="alert alert-info"><?php _e('Your comment is awaiting approval.','everbox');?></div>
        	<?php endif; ?>

	        <?php comment_text(); ?>
    	</div>
    </li>
    <!-- End commment -->
    <?php
}

function everbox_comment_form($defaults) {
    $defaults['comment_notes_before'] = '';
    $defaults['comment_notes_after'] = '';
    $defaults['id_form'] = 'form';
    $defaults['comment_field'] = '<div class="form-group">
									<textarea placeholder="Your comment" required name="comment" id="comment" class="form-control form-message" rows="6"></textarea>
								  </div>';
    return $defaults;
}
// add_filter('comment_form_defaults','everbox_comment_form');

function everbox_comment_fields() {
    $commenter = wp_get_current_commenter();
    $aria_req  = "aria-required='true'";

    $fields = array(
        'author' => '<div class="form-group"><div class="row"><div class="col-xs-6">
        				<input type="text" id="author" placeholder="Name" name="author" value="' . esc_attr($commenter['comment_author']) . '" '.$aria_req .'class="form-control" />
        			</div>',
        'email'  => '<div class="col-xs-6">
        				<input type="text" id="email" placeholder="Email" name="email" value="' . esc_attr($commenter['comment_author_email']) . '" '.$aria_req .' class="form-control" />
        			</div></div></div>'
    );

    return $fields;
}
// add_filter('comment_form_default_fields','everbox_comment_fields');

/**
 * Posts pagination
 */
function everbox_posts_pagination() {
    global $everbox_options;
    if( !empty($everbox_options['infinite_pagination']) && $everbox_options['infinite_pagination'] ) {
        return;
    }
    $output = '';
    $output .= '<div class="pagination-wrap">';
    $output .= get_the_posts_pagination(array('mid_size' => 1, 'screen_reader_text' => ''));
    $output .= '</div>';
    echo $output;
}

/**
 * Disable Google Fonts
 */
function everbox_disable_google_fonts($translations, $text, $context, $domain ) {
    global $everbox_options;
    if(isset($everbox_options['disable_google']) && $everbox_options['disable_google']) {
        return $translations;
    }
    $google_fonts_contexts = array('Open Sans font: on or off','Lato font: on or off','Source Sans Pro font: on or off','Bitter font: on or off');
    if( $text == 'on' && in_array($context, $google_fonts_contexts ) ){
        $translations = 'off';
    }

    return $translations;
}
add_filter( 'gettext_with_context', 'everbox_disable_google_fonts', 888, 4);